package com.aquarium.ui.controller;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.TextView;

import com.aquarium.R;
import com.aquarium.data.ModuleSettingDataBase;
import com.aquarium.module.ModuleControlService;

public class DosingPumpSetting extends ControllerSetting {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mTitle.setText(getResources().getString(R.string.dosing_pump));
	}

	@Override
	protected void onResume() {
		super.onResume();
		invalidateListView();
	}

	@Override
	public void createListViewAdapter() {
		mTimePickListViewAdapter = new TimePickListViewAdapter(this,
				R.layout.dosing_pump_setting_row,
				mModuleSettingDataBase
						.getCursor(ModuleSettingDataBase.DOSING_PUMP_TABLE),
				new String[] { ModuleSettingDataBase.HOUR,
						ModuleSettingDataBase.MINUTE,
						ModuleSettingDataBase.PUMP_1_ON_OFF,
						ModuleSettingDataBase.PUMP_2_ON_OFF,
						ModuleSettingDataBase.PUMP_3_ON_OFF,
						ModuleSettingDataBase.PUMP_4_ON_OFF,
						ModuleSettingDataBase.MON, ModuleSettingDataBase.TUES,
						ModuleSettingDataBase.WED, ModuleSettingDataBase.THUR,
						ModuleSettingDataBase.FRI, ModuleSettingDataBase.SAT,
						ModuleSettingDataBase.SUN }, new int[] {
						R.id.start_time_txv, R.id.duration_txv,
						R.id.launch_pump_txv, R.id.mon_txv, R.id.tues_txv,
						R.id.wed_txv, R.id.thur_txv, R.id.fri_txv,
						R.id.sat_txv, R.id.sun_txv });
	}

	@Override
	public void setListeners() {
		super.setListeners();
		mAddButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(ACTION_ADD_SETTING);
				intent.setClass(DosingPumpSetting.this,
						DosingPumpTimePicker.class);
				startActivity(intent);
			}
		});
		mDeleteButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				mModuleSettingDataBase.deleteDosingPunpData(mDeleteList);
				invalidateListView();
				Intent intent = new Intent(
						ModuleControlService.ACTION_UPDATE_DOSING_PUMP_DATA);
				sendBroadcast(intent);
			}
		});

		mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterview, View view,
					int position, long id) {
				Intent intent = new Intent(ACTION_MODIFY_SETTING);
				intent.setClass(DosingPumpSetting.this,
						DosingPumpTimePicker.class);
				intent.putExtra(LISTVIEW_CLICKED_POSITION, position);
				startActivity(intent);
			}
		});
	}

	@Override
	public void setListViewBinder() {

		mTimePickListViewAdapter
				.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
					@Override
					public boolean setViewValue(View view, Cursor cursor,
							int columnIndex) {
						boolean result = bindListView(view, cursor);
						int id = view.getId();
						if (id == R.id.launch_pump_txv) {

							boolean[] bools = mModuleSettingDataBase
									.getLaunchPump(cursor);
							StringBuffer sb = new StringBuffer();
							for (int i = 1; i <= 4; i++) {
								if (bools[i - 1] == true) {
									sb.append(i + ", ");
								}
							}
							if (sb.length() > 2) {
								((TextView) view).setText(sb.substring(0,
										sb.length() - 2));
							}

							return true;
						}
						return result;
					}
				});
	}

	private void invalidateListView() {
		mTimePickListViewAdapter.changeCursor(mModuleSettingDataBase
				.getCursor(ModuleSettingDataBase.DOSING_PUMP_TABLE));
		mListView.invalidate();
	}
}
